package pl.edu.agh.gt.util;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class BetterRandom {

	private static Map<String, SecureRandom> rng = new HashMap<String, SecureRandom>();

	public static double nextDouble(String key) {
		SecureRandom sr = rng.get(key);
		if(sr != null)
			return sr.nextDouble();
		else {
			sr = new SecureRandom();
			rng.put(key, sr);
			return sr.nextDouble();
		}
	}
	
	public static double nextDouble(String key, double lowerBound, double upperBound) {
		SecureRandom sr = rng.get(key);
		if(sr != null)
			return sr.nextDouble() * (upperBound - lowerBound) + lowerBound;
		else {
			sr = new SecureRandom();
			rng.put(key, sr);
			return sr.nextDouble() * (upperBound - lowerBound) + lowerBound;
		}
	}
	
	public static float nextFloat(String key) {
		SecureRandom sr = rng.get(key);
		if(sr != null)
			return sr.nextFloat();
		else {
			sr = new SecureRandom();
			rng.put(key, sr);
			return sr.nextFloat();
		}
	}
	
	public static float nextFloat(String key, float lowerBound, float upperBound) {
		SecureRandom sr = rng.get(key);
		if(sr != null)
			return sr.nextFloat() * (upperBound - lowerBound) + lowerBound;
		else {
			sr = new SecureRandom();
			rng.put(key, sr);
			return sr.nextFloat() * (upperBound - lowerBound) + lowerBound;
		}
	}
	
	public static int nextInt(String key, int upBound) {
		SecureRandom sr = rng.get(key);
		if(sr != null)
			return sr.nextInt(upBound);
		else {
			sr = new SecureRandom();
			rng.put(key, sr);
			return sr.nextInt(upBound);
		}
	}
	
	public static boolean nextBoolean(String key) {
		SecureRandom sr = rng.get(key);
		if(sr != null)
			return sr.nextBoolean();
		else {
			sr = new SecureRandom();
			rng.put(key, sr);
			return sr.nextBoolean();
		}
	}
	
	public static Random getInstance(String key) {
		SecureRandom sr = rng.get(key);
		if(sr != null)
			return sr;
		else {
			sr = new SecureRandom();
			rng.put(key, sr);
			return sr;
		}
	}
}
